package com.estate.web.controller.system;

import com.estate.common.annotation.Log;
import com.estate.common.constant.Constants;
import com.estate.common.core.controller.BaseController;
import com.estate.common.core.domain.AjaxResult;
import com.estate.common.core.domain.entity.SysDept;
import com.estate.common.core.domain.req.SysDeptAddReq;
import com.estate.common.core.domain.req.SysDeptEditReq;
import com.estate.common.core.domain.resp.SysDeptResp;
import com.estate.common.core.domain.resp.SysTeamResp;
import com.estate.common.enums.BusinessType;
import com.estate.common.utils.StringUtils;
import com.estate.common.utils.bean.BeanUtils;
import com.estate.system.service.ISysDeptService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * team信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController
{
    @Autowired
    private ISysDeptService deptService;

    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    public AjaxResult list(SysDept dept)
    {
        return success(deptService.selectDeptTree(dept));
    }

    /**
     * 获取部门树列表
     */
    @GetMapping("/treeOptions")
    public AjaxResult treeOptions()
    {
        SysDept dept = new SysDept();
        dept.setType(Constants.DeptType);
        return success(deptService.selectDeptTreeOptions(dept));
    }

    @GetMapping("/listTeamByDeptId/{deptId}")
    public AjaxResult getDeptTeamList(@PathVariable Long deptId) {
        SysDept sysDept = new SysDept();
        sysDept.setType(Constants.TeamType);
        sysDept.setParentId(deptId);
        List<SysDept> list = deptService.selectDeptList(sysDept);
        return success(
                list.stream()
                        .map(item -> {
                            SysTeamResp sysTeamResp = new SysTeamResp();
                            BeanUtils.copyBeanProp(sysTeamResp, item);
                            return sysTeamResp;
                        })
                        .collect(Collectors.toList())
        );
    }

    /**
     * 根据id获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        deptService.checkDeptDataScope(id);
        SysDept sysDept = deptService.selectDeptById(id);
        SysDeptResp sysDeptResp = new SysDeptResp();
        BeanUtils.copyBeanProp(sysDeptResp, sysDept);
        sysDeptResp.setId(sysDept.getDeptId());
        sysDeptResp.setName(sysDept.getDeptName());
        return success(sysDeptResp);
    }

    /**
     * 新增
     */
    @Log(title = "Org Management", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDeptAddReq req)
    {
        SysDept dept = new SysDept();
        BeanUtils.copyBeanProp(dept, req);
        dept.setDeptName(req.getName());
        if (StringUtils.isEmpty(req.getType())) {
            dept.setType(Constants.DeptType);
        } else if(!Lists.newArrayList(Constants.DeptType, Constants.TeamType).contains(req.getType())) {
            return error("The value of type is error");
        }
        if (!deptService.checkDeptNameUnique(dept))
        {
            return error("Addition failed, " + dept.getDeptName() + " already exists");
        }
        dept.setCreateBy(getUsername());
        return toAjax(deptService.insertDept(dept));
    }

    /**
     * 修改
     */
    @Log(title = "Org Management", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDeptEditReq req)
    {
        SysDept dept = new SysDept();
        BeanUtils.copyBeanProp(dept, req);
        Long id = req.getId();
        dept.setDeptId(id);
        dept.setDeptName(req.getName());
        deptService.checkDeptDataScope(id);
        if (!deptService.checkDeptNameUnique(dept))
        {
            return error("Update failed, " + dept.getDeptName() + " already exists");
        }
        else if (dept.getParentId().equals(id))
        {
            return error("The parentId cannot be the same as the id");
        }
        dept.setUpdateBy(getUsername());
        return toAjax(deptService.updateDept(dept));
    }

    /**
     * 删除
     */
    @Log(title = "Org Management", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        if (deptService.hasChildByDeptId(id))
        {
            return warn("Subordinates exist, deletion is not allowed");
        }
        if (deptService.checkDeptExistUser(id))
        {
            return warn("Department/team contains users, deletion is not allowed");
        }
        deptService.checkDeptDataScope(id);
        return toAjax(deptService.deleteDeptById(id));
    }
}
